/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Exercise1Project.Models; // needed namespace ref's
using Exercise1Project.Models.Validation;
using System.Web.Mvc;
using System.Net;
using System.Data.Entity;

namespace Exercise1Project.Controllers
{


    public class BikeController : Controller
    {
        private BicycleRepairEntities db = new BicycleRepairEntities(); // db is a database context which interacts with the database and gives access to bike table

        private IBusinessLogicService _service;  // instance of business logic  interface

        public BikeController() // non parameterised constructor used for processing views
        {
            _service = new BusinessLogicService(new ModelStateWrapper(this.ModelState));
        }

        public BikeController(IBusinessLogicService service)  // parameterised constructor used for test purposes
        {
            _service = service;
        }

        //
        // GET: /Bike/
        [HttpGet]
        public ActionResult Index()  // list of a customers bicycles
        {
            string id = TempData["CustomerId"].ToString();
            TempData.Keep("CustomerId");

            var listOfCustomerBikes = _service.GetBikeByCustomerID(id);

            if (listOfCustomerBikes == null)
            {
                return HttpNotFound();
            }

            return View("Index", listOfCustomerBikes);
        }

        //GET: /bike/create
        [HttpGet]
        public ActionResult Create()
        {
            return View("Create");
        }

        //POST: /bike/create
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "BikeID")] Bike bikeToAdd)  // do not bind bikeID as we do not wish to set it (it's auto in the database)
        {
            if (ModelState.IsValid)
            {
                if (_service.AddCustomerBike(bikeToAdd))
                    return RedirectToAction("Index", new { id = bikeToAdd.CustomerID });
            }
            return View("Create", bikeToAdd); 
        }

        //GET: /Bike/Edit
        [HttpGet]
        public ActionResult Edit(int id)
        {
            Bike bikeToEdit = _service.GetBikeByBikeId(id);
            if (bikeToEdit == null)
            {
                return HttpNotFound();
            }
            return View("Edit", bikeToEdit);
        }

        //POST:  / bike/Edit 
        [HttpPost]
        public ActionResult Edit(Bike bikeToEdit)
        {
            if (ModelState.IsValid)
            {
                if (_service.UpdateBikeDetail(bikeToEdit))
                    return RedirectToAction("Index", new { id = bikeToEdit.CustomerID });
            }
            return View("Edit", bikeToEdit);             
        } // close of actionresult postEdit 

        //GET: /bike/delete/
        [HttpGet]
        public ActionResult Delete(int id)
        {
            Bike bikeToRemove = _service.GetBikeByBikeId(id);
            if (bikeToRemove == null)
            {
                return HttpNotFound();
            }
            return View("Delete", bikeToRemove); 
        }

        //POST: /bike/delete
        [HttpPost]
        public ActionResult Delete(int id, FormCollection formCollection)  // string x not needed but found necessary for some reason to distinguish between delete methods signatures
        {
             Bike bikeToRemove = _service.GetBikeByBikeId(id);

             if (_service.RemoveCustomerBike(bikeToRemove.BikeID))
                 return RedirectToAction("Index", new { id = bikeToRemove.CustomerID });
             return View("Delete", new { id = bikeToRemove.BikeID });

        }

        // GET: / bike/ list of services
        public ActionResult BikeService(int id)      // redirect to BikeService Controller - ServiceList action
        {
           
           Bike bike = _service.GetBikeByBikeId(id);  // take selected bike details over to bikeservice controller index method
           TempData["bike_id"] = bike.BikeID;
           TempData["bike_model"] = bike.Make;
           TempData["bike_make"] = bike.Model;
           TempData["bike_color"] = bike.Color;

           return RedirectToAction("ServiceList", "BikeService", new {id = bike.BikeID });
        }

        public ActionResult RedirectToCustomerDetail()
        {
            string id = TempData["CustomerId"].ToString();
            TempData.Keep("CustomerId");
            return  RedirectToAction("CustomerDetail", "CustomerDetails", new { id = id });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        } 


	} // close of class controller

} // close of namespace 
